// pages/netdoctors/list/list.js
var docotrservice = require("../../../apiservice/doctor/doctorservice");
const jsonData = require("../jsondata.js");

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		activeIcon: "../../../images/pages/check_box-check.png",
		inactiveIcon: "../../../images/pages/check_box.png",
		space: " ",
		alphabet: [],
		isMoreThan500: false,
		livenessArray: jsonData.livenessArray,
		livenessIndex: 0,
		liveness: '',
		nameSearch: '',
		districtandcounty: '',
		areaArray: [],
		titleArray: jsonData.titleArray,
		currentAreaName: '请选择区域',
		areaIndex: 0,
		areaId: '',
		opentype: 'detail', //
		mlChecked: false,
		startTime: '1900-01-01',
		endTime: '2999-12-31',
		type: 0,
		noshow: "系统没有找到您要的结果，请重新查询",
		rows: 1,
		height: wx.getSystemInfoSync().windowHeight - 150
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		
		var that = this;
		if (that.options.startTime && that.options.endTime) {
		
			that.setData({
				startTime: that.options.startTime,
				endTime: that.options.endTime,
				type: that.options.type,
				
			});
		}
		if(options.opentype){
			that.setData({
				opentype:options.opentype
			})
		}
		
		

		const eventChannel = this.getOpenerEventChannel();

		eventChannel.on('openedDoctorPage', function (e) {
			//debugger;
			that.setData({
				opentype: e.data,
			});
		});

		//获取数据
		this.GetAreaByConditions();

		this.GetDoctorListByConditions(this.data.liveness);
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		// var that = this;
		// //如果成学术推广首页点击详情进来
		// if (that.data.startTime &&
		// 	that.data.endTime &&
		// 	that.data.type && this.data.nameSearch) {
		// 	this.GetDoctorListByConditions(this.data.liveness);
		// } else { 
		// 	docotrservice.GetDoctorList().then(function(success) {
		// 		console.log(success);
		// 		that.initailAlphabet(that, success.data.datas);
		//     if (success.data.existsnextdata) {
		//       wx.showToast({
		//         title: '系统仅显示前500条数据；请缩小查询范围,以更精确查找。',
		//         icon:'none'
		//       })
		//     }
		// 	}).catch(function(ex) {
		// 		console.log(ex);
		// 	})
		// }
	},


	onSearch: function (e) {
		this.GetDoctorListByConditions(this.data.liveness);
	},
	onChange: function (e) {
		console.log('onChange', e)
	},

	addDoctor: function () {
		wx.navigateTo({
			url: '/pages/doctor/add/add'
		})
	},

	enterDetail: function (e) {
		if (this.data.opentype == "choose") {
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.emit('DoctorEvent', {
				doctorname: e.currentTarget.dataset.name,
				doctorid: e.currentTarget.id,
				hospitalid: e.currentTarget.dataset.hospitalid
			});
			wx.navigateBack({
				delta: 1
			})
		} else {
			wx.navigateTo({
				url: '/pages/doctor/detail/detail?doctorId=' + e.currentTarget.id
			})
		}
	},
	bindAreaChange: function (e) {
		var index = e.detail.value
		var areaItem = this.data.areaArray[index];
		var areaname = areaItem.name;
		var areaid = areaItem.id;
		//console.log(areaid);
		this.setData({
			areaIndex: index,
			currentAreaName: areaname,
			areaId: areaid
		})

	},
	bindLivenessChange: function (e) {
		var index = e.detail.value
		var livenessItem = this.data.livenessArray[index];
		var liveness = livenessItem.id;
		//console.log(areaid);
		this.setData({
			livenessIndex: index,
			liveness: liveness
		})
		this.GetDoctorListByConditions(liveness);
	},
	onNameSearchChange: function (e) {
		//console.log(e);
		this.setData({
			nameSearch: e.detail
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},


	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},
	onMLChange: function (e) {
		this.setData({
			mlChecked: e.detail
		});
	},


	/**
	 * 列表赋值
	 */
	initailAlphabet: function (that, groupdata) {
		//debugger;
		// const alphabet = []
		// groupdata.forEach(element => {
		// 	var initial = element.groupname;
		// 	var cells = element.grouplist;
		// 	alphabet.push({
		// 		initial,
		// 		cells
		// 	})
		// });
		// that.setData({
		// 	alphabet: alphabet
		// });
		this.setData({ alphabet: [] });
		for (let index in groupdata) {
			this.setData({ ['alphabet[' + index + ']']: { initial: groupdata[index].groupname, cells: groupdata[index].grouplist } });
		}
	},
	GetDoctorListByConditions: function (liveness) {
		//debugger;
		var that = this;
		docotrservice.GetDoctorListByConditions(liveness, this.data.areaId, this.data.mlChecked, this.data.startTime, this
			.data.endTime, this.data.type, this.data.nameSearch).then(function (success) {
				console.log(success.data);
				that.setData({
					rows: success.data.datas.length,
				});
				that.initailAlphabet(that, success.data.datas);
				if (success.data.existsnextdata) {
					wx.showToast({
						title: '系统仅显示前500条数据；请缩小查询范围,以更精确查找。',
						icon: 'none'
					})
				}
			}).catch(function (ex) {
				console.log(ex);
			})
	},
	/**获取地区**/
	GetAreaByConditions: function () {
		var that = this;
		docotrservice.GetAreaByConditions(this.data.liveness, this.data.districtandcounty).then(function (success) {
			var list = success.data;
			var areaArray = [{
				id: '',
				name: '全部区域'
			}];
			for (var i = 0; i < list.length; i++) {
				var name = list[i].new_districtandcountyName;
				var id = list[i].new_districtandcounty;
				var obj = {
					id: id,
					name: name
				};
				areaArray.push(obj);
			}
			that.setData({
				areaArray: areaArray
			})
		}).catch(function (ex) {
			console.log(ex);
		})
	}
})
